

import time
import logging


import rpyc

from dexen.system import util
from dexen.system.base import service, task as taskbase


def connect():
    global conn
    conn = rpyc.connect_by_service(service_name="AGENT_MANAGER",
                                   config=service.default_config)

def create_schema(schema_name, schema_dir):
    schema_zip = util.get_schema_zip(schema_dir)
    logging.debug("The size of schema zip %d", len(schema_zip))
    conn.root.create_schema(schema_name, schema_zip)

def get_tasks(schema_name):
    tasks = conn.root.get_tasks(schema_name)
    tasks = rpyc.classic.obtain(tasks)
    return tasks

def get_schema_names():
    schema_names = conn.root.get_schema_names()
    schema_names = rpyc.classic.obtain(schema_names)
    return schema_names

def run_schema(schema_name, tasks):
    conn.root.run_schema(schema_name, tasks)

def get_schema_states():
    schema_states = rpyc.classic.obtain(conn.root.get_schema_states())
    return schema_states

def delete_schema(schema_name):
    conn.root.delete_schema(schema_name)


def main():
    logging.basicConfig(level=logging.DEBUG)
    conn = rpyc.connect_by_service(service_name="AGENT_MANAGER",
                                   config=service.default_config)

    schema_name = "pbv1"    

    schema_dir = "/home/dexen/workspace/dexen_on_google/dexen/user/schemas"
    schema_dir += "/packing_boxes_v1"
    schema_zip = util.get_schema_zip(schema_dir)
    logging.debug("The size of schema zip %d", len(schema_zip))
    #conn.root.test()
    #conn.root.test2()
    
    conn.root.create_schema(schema_name, schema_zip)
    tasks = conn.root.get_tasks(schema_name)
    tasks = rpyc.classic.obtain(tasks)
    
    for task in tasks:
        if task.classname == "FeedbackTask":
            task.input_size = 400
        elif task.type == taskbase.NonInitType:
            task.input_size = 20
        print task
            
    conn.root.run_schema(schema_name, tasks)
    #conn.root.run_schema(schema_name, tasks)
    #conn.root.run_schema(schema_name, {})
    while True:
        pass
    
if __name__ == "__main__":
    main()